home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.001 / crossfir~ / eutl / errlib / errlib.c < prev    next >
C/C++ Source or Header  |  1994-11-05  |  4KB  |  135 lines

  1. #include "libc.h"
  2. #include "errlib.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <signal.h>
  6. #include <errno.h>
  7.  
  8. /*
  9.  *   eutl - A collection of useful libraries
  10.  *          This part is errlib -- a unified error handling library.
  11.  *
  12.  *   (c) Copyright 1993 Eric Anderson 
  13.  *
  14.  * My thanks to Geoffrey Collyer and Henry Spencer for providing the basis
  15.  * for this copyright.
  16.  *
  17.  * Permission is granted to anyone to use this software for any purpose on
  18.  * any computer system, and to alter it and redistribute it freely, subject
  19.  * to the following restrictions:
  20.  *
  21.  * 1. The authors are not responsible for the consequences of use of this
  22.  *    software, no matter how awful, even if they arise from flaws in it.
  23.  *
  24.  * 2. The origin of this software must not be misrepresented, either by
  25.  *    explicit claim or by omission.  Since few users ever read sources,
  26.  *    credits must appear in the documentation.
  27.  *
  28.  * 3. Altered versions must be plainly marked as such, and must not be
  29.  *    misrepresented as being the original software.  Since few users
  30.  *    ever read sources, credits must appear in the documentation.
  31.  *
  32.  * 4. This notice may not be removed or altered.
  33.  */
  34.  
  35. char *errlib_packagever = "ErrLib V1.0";
  36. char *errlib_EOverflowBuffer = "Error Message Buffer Overflow";
  37. char *internal_Einternal = "Internal Package error";
  38.  
  39. char *exception_progname = "Unknown Program";
  40. char *exception_package = NULL;
  41. char *exception_errmsg = NULL;
  42. char *exception_eid = NULL;
  43.  
  44. exceptionStruct *__errlib_top = NULL;
  45.  
  46. static void DoLongJmpErf(int printerr,char *package,char *errid, char *errfmt,
  47.              va_list args)
  48. {
  49.   static char buf[1024];
  50.  
  51.   buf[1024] = '\0';
  52.   vsprintf(buf, errfmt, args);
  53.   if (buf[1024]!= '\0') {
  54.     if (printerr)
  55.       fprintf(stderr,
  56.           "%s: Error in package %s: Internal sprintf buffer overflow.\n",
  57.           exception_progname,errlib_packagever);
  58.     throwerr(errlib_packagever,
  59.          errlib_EOverflowBuffer,"Internal sprintf buffer overflow\n");
  60.   }
  61.   if (printerr)
  62.     fprintf(stderr,"%s: Error in package %s: %s",
  63.         exception_progname,package,buf);
  64.   throwerr(package,errid,buf);
  65. }
  66.  
  67. void LongJmpPrintErrorFunction(char *package,char *errid,char *errfmt, ...)
  68. {
  69.   va_list args;
  70.  
  71.   va_start(args,errfmt);
  72.   DoLongJmpErf(1,package,errid,errfmt,args);
  73. }
  74.  
  75. void LongJmpErrorFunction(char *package,char *errid,char *errfmt, ...)
  76. {
  77.   va_list args;
  78.  
  79.   va_start(args,errfmt);
  80.   DoLongJmpErf(0,package,errid,errfmt,args);
  81. }
  82.  
  83. static void DoAbortErrorFunction(char *package,char *errid,char *errfmt,
  84.                  va_list args)
  85. {
  86.   (void)fprintf(stderr,"Fatal Error in package %s:",package);
  87.   vfprintf(stderr,errfmt,args);
  88.   exit(1);
  89.   fprintf(stderr,"Some dufus is catching abort signals and not aborting\n");
  90.   signal(SIGABRT,SIG_DFL);
  91.   abort();
  92. }
  93.  
  94. void AbortErrorFunction(char *package,char *errid,char *errfmt, ...)
  95. {
  96.   va_list args;
  97.   va_start(args,errfmt);
  98.   DoAbortErrorFunction(package,errid,errfmt,args);
  99. }
  100.  
  101. void throwerr(char *package,char *errid,char *buf)
  102. {
  103.   exception_package = package;
  104.   exception_eid = errid;
  105.   exception_errmsg = buf;
  106.   if (__errlib_top == NULL) {
  107.     AbortErrorFunction(errlib_packagever,"unhandled exception",
  108.                "%s::ErrLib: Unhandled Exception in Package %s.\n   %s",
  109.                exception_progname,package,buf);
  110.   }
  111.   if (__errlib_top->cur != __errlib_top) {
  112.     AbortErrorFunction(errlib_packagever,"mangled exception",
  113.                "%s::ErrLib: Exception Stack Mangled -- Exception in %s.\n   %s",
  114.                exception_progname,package,buf);
  115.   }
  116.   longjmp(__errlib_top->towhere,1);
  117. }
  118.  
  119. void errlib_setprogname(char *progname)
  120. {
  121.   exception_progname = progname;
  122. }
  123.  
  124. #ifdef sun
  125. extern int sys_nerr;
  126. extern char *sys_errlist[];
  127.  
  128. char *strerror(int errno)
  129. {
  130.   if (errno<0 || errno>sys_nerr)
  131.     return "Unknown Error Code";
  132.   return sys_errlist[errno];
  133. }
  134. #endif
  135.